﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.DataLayer;

namespace UComment.Domain
{
    public class Comment
    {
        public delegate void CommentCreatedEventHandler(Comment sender, EventArgs e);
        public delegate void CommentSpamEventHandler(Comment sender, EventArgs e);

        public static event CommentCreatedEventHandler CommentCreated;
        public static event CommentSpamEventHandler CommentSpam;

        protected virtual void OnCommentCreated(EventArgs e)
        {
            if (CommentCreated != null) CommentCreated(this, e);
        }

        protected virtual void OnCommentSpam(EventArgs e)
        {
            if (CommentSpam != null) CommentSpam(this, e);
        }

        public int ParentNodeId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Website { get; set; }
        public bool Spam { get; set; }
        public DateTime Created { get; set; }
        public string CommenText { get; set; }


        public static Comment MakeNew(int parentNodeId, string name, string email, string website, bool spam, DateTime created, string commentText) {
            
            Comment c = new Comment();

            c.ParentNodeId = parentNodeId;
            c.Name = name;
            c.Email = email;
            c.Website = website;
            c.Spam = spam;
            c.Created = created;
            c.CommenText = commentText;

            ISqlHelper SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
            SqlHelper.ExecuteNonQuery(
                @"insert into Comment(mainid,nodeid,name,email,website,comment,spam,created) 
                        values(@mainid,@nodeid,@name,@email,@website,@comment,@spam,@created)",
                 SqlHelper.CreateParameter("@mainid", -1),
                SqlHelper.CreateParameter("@nodeid", c.ParentNodeId),
                SqlHelper.CreateParameter("@name", c.Name),
                SqlHelper.CreateParameter("@email", c.Email),
                SqlHelper.CreateParameter("@website", c.Website),
                SqlHelper.CreateParameter("@comment", c.CommenText),
                SqlHelper.CreateParameter("@spam", c.Spam),
                SqlHelper.CreateParameter("@created", c.Created));

            c.OnCommentCreated(EventArgs.Empty);

            if (c.Spam)
            {
                c.OnCommentSpam(EventArgs.Empty);
            }

            return c;
        }

        public override string ToString()
        {

            return @"ParentNodeId " + ParentNodeId + @"
            Name " + Name + @"
            Email " + Email + @"
            Website " + Website + @"
            Spam " + Spam + @"
            Created "+ Created + @"
            CommenText " + CommenText + Environment.NewLine;

        }
        

    }
}